package Leo.service.impl;

import Leo.dao.SystemsettingDao;
import Leo.entity.table.Systemsetting;
import Leo.redis.RedisUtil;
import Leo.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service("RedisService")
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    SystemsettingDao systemsettingDao;

    @Override
    public void saveValue(String key, Object value) {
        redisUtil.save(key,value);
    }

    @Override
    public Object getValue(String key) {
        Object fromRedis = redisUtil.getFromRedis(key);
        return fromRedis;
    }

    @Override
    public String getStringValue(String key) {
        Object fromStringRedis = redisUtil.getFromStringRedis(key);
        return fromStringRedis.toString();
    }

    @Override
    @CachePut(value="sys",key = "#systemsetting.settingId + #systemsetting.settingCode")
    public Systemsetting cacheSave(Systemsetting systemsetting) {
        systemsettingDao.updateByPrimaryKey(systemsetting);
        System.out.println("写入缓存");
        return systemsetting;
    }

    @Override
    @Cacheable(value="sys",key = "#settingId + #settingCode")
    public Systemsetting cacheFind(Long settingId,String settingCode) {
        System.out.println("在数据库中查找");
        return systemsettingDao.selectByPrimaryKey(settingId);
    }

    @Override
    @CacheEvict(value="sys",key = "#settingId + #settingCode")
    public void cacheRemove(Long settingId,String settingCode) {
        System.out.println("删除了redis缓存数据");
    }
}
